package com.zxr.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zxr.common.annotation.Log;
import com.zxr.common.enums.BusinessType;
import com.zxr.system.domain.Contracts;
import com.zxr.system.service.IContractsService;
import com.zxr.common.core.controller.BaseController;
import com.zxr.common.core.domain.AjaxResult;
import com.zxr.common.utils.poi.ExcelUtil;
import com.zxr.common.core.page.TableDataInfo;

/**
 * 合同Controller
 * 
 * @author zxrproject
 * @date 2025-04-26
 */
@Controller
@RequestMapping("/system/contracts")
public class ContractsController extends BaseController
{
    private String prefix = "system/contracts";

    @Autowired
    private IContractsService contractsService;

    @RequiresPermissions("system:contracts:view")
    @GetMapping()
    public String contracts()
    {
        return prefix + "/contracts";
    }

    /**
     * 查询合同列表
     */
    @RequiresPermissions("system:contracts:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Contracts contracts)
    {
        startPage();
        List<Contracts> list = contractsService.selectContractsList(contracts);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @RequiresPermissions("system:contracts:export")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Contracts contracts)
    {
        List<Contracts> list = contractsService.selectContractsList(contracts);
        ExcelUtil<Contracts> util = new ExcelUtil<Contracts>(Contracts.class);
        return util.exportExcel(list, "合同数据");
    }

    /**
     * 新增合同
     */
    @RequiresPermissions("system:contracts:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存合同
     */
    @RequiresPermissions("system:contracts:add")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Contracts contracts)
    {
        return toAjax(contractsService.insertContracts(contracts));
    }

    /**
     * 修改合同
     */
    @RequiresPermissions("system:contracts:edit")
    @GetMapping("/edit/{contractId}")
    public String edit(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        Contracts contracts = contractsService.selectContractsByContractId(contractId);
        mmap.put("contracts", contracts);
        return prefix + "/edit";
    }

    /**
     * 修改保存合同
     */
    @RequiresPermissions("system:contracts:edit")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Contracts contracts)
    {
        return toAjax(contractsService.updateContracts(contracts));
    }

    /**
     * 删除合同
     */
    @RequiresPermissions("system:contracts:remove")
    @Log(title = "合同", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(contractsService.deleteContractsByContractIds(ids));
    }
}
